package com.imay.live.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.imay.base.activity.BaseActivity;
import com.imay.live.activity.user.ResigterActivity;
import com.imay.live.cache.UserInfoCache;
import com.imay.live.helper.EventHelper;
import com.imay.live.model.UserInfoModel;
import com.imay.live.utils.ToastUtil;
import com.imay.live.R;
import com.imay.tbas_api.IMYTBASObject;

/**
 * create by nick
 *  我的->设置—>账号安全
 */
public class AccountSecurityActivity extends BaseActivity implements View.OnClickListener {


    private TextView tv_account_phone;
    private TextView tv_account_bind;
    private View view_line;
    private TextView tv_account_reset_psw;
    private IMYTBASObject userInfoChangeListener;
    private int bindPhoneType= ResigterActivity.STATUS_BIND_PHONE;

    public static void start(Context context) {
        Intent starter = new Intent(context, AccountSecurityActivity.class);
//        starter.putExtra();
        context.startActivity(starter);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_account_security;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setTitle(getString(R.string.setting_account));
        bindViews();
        refreshView();
        initUserInfoChangeListener();

    }



    private void bindViews() {

        tv_account_phone = (TextView) findViewById(R.id.tv_account_phone);
        tv_account_bind = (TextView) findViewById(R.id.tv_account_bind);
        view_line =  findViewById(R.id.view_line);
        tv_account_reset_psw = (TextView) findViewById(R.id.tv_account_reset_psw);
        $(R.id.rl_account_phone).setOnClickListener(this);
        tv_account_reset_psw.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_account_phone:
                ResigterActivity.startActivity(this,bindPhoneType);
                break;
            case R.id.tv_account_reset_psw:
                PasswordResetActivity.start(this);
                break;
        }

    }
    private void refreshView(){
        UserInfoModel infoModel = UserInfoCache.getUserInfo();
        if (infoModel==null){
            return ;
        }
        if (infoModel.getPhone()==0){
            tv_account_reset_psw.setVisibility(View.GONE);
            tv_account_phone.setText("");
            tv_account_bind.setTextColor(getResources().getColor(R.color.unread_point_bg));
            tv_account_bind.setText(R.string.setting_account_unbind);
        }else{
            tv_account_reset_psw.setVisibility(View.VISIBLE);
            tv_account_phone.setText(infoModel.getPhone()+"");
            tv_account_bind.setTextColor(getResources().getColor(R.color.text_75));
            tv_account_bind.setText(R.string.setting_account_change_bind);
            bindPhoneType = ResigterActivity.STATUS_MODIFY_PHONE;

        }

    }

    private void initUserInfoChangeListener(){
        userInfoChangeListener = new IMYTBASObject(false);
        userInfoChangeListener.stringCommand= EventHelper.EVENT_UPDATE_USER_INFO;
        userInfoChangeListener.setDataResultListener(new IMYTBASObject.OnDataResultListener() {
            @Override
            public void onCommandTaskSuccess(IMYTBASObject imytbasObject) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        refreshView();
                        ToastUtil.show(AccountSecurityActivity.this,R.string.bind_success);
                    }
                });
            }
        });
        userInfoChangeListener.addEventListenWithTBASObject();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (userInfoChangeListener!=null){
            userInfoChangeListener.destroy();
        }
    }


}
